<?php

namespace app\api\controller;

use app\api\model\Oauth;
use app\api\model\TieBa;
use app\api\model\TieBaComment AS CommentModel;
use app\api\validate\Page;

class TieBaComment extends Base
{
    /**
     * 我的消息列表
     * @return array
     * @throws \think\Exception\DbException
     */
    public function index()
    {
        $page = $data['page'] = input('page') ? input('page') : 1;
        $size = $data['size'] = input('size') ? input('size') : $this->pageSize;

        // 检测变量
        $validate = new Page();
        if (!$validate->check($data)) {
            return self::sendResult(102, [], $validate->getError());
        }

        $oauthId = $this->oauthInfo['id'];
        $model = CommentModel::myMessage($oauthId, $page, $size);
        return self::sendResult(self::$okCode, $model);
    }

    /**
     * 评论或者回复
     * @param $tieId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function create($tieId)
    {
        $toUid = input('toUid');
        $content = input('content');

        if (empty($content)) {
            return self::sendResult(100, [], '请输入内容');
        }

        if ($tieId < 0) {
            return self::sendResult(118, [], '帖子不存在');
        }

        $tie = TieBa::getOneById($tieId);
        if (empty($tie)) {
            return self::sendResult(118, [], '帖子不存在');
        }

        $toUser = [];
        if ($toUid > 0) {
            $toUser = Oauth::getOne($toUid);
            if (empty($toUser)) {
                return self::sendResult(110);
            }
        }

        $oauthInfo = $this->oauthInfo;

        // 添加评论或者回复
        CommentModel::addOne($oauthInfo, $tie, $content, $toUser);

        return self::sendResult(self::$okCode, [], $toUid > 0 ? '回复成功' : '评论成功');
    }

    /**
     * 删除自己的评论或者回复
     * @param $commentId
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function delete($commentId)
    {
        if ($commentId <= 0) {
            return self::sendResult(102);
        }

        $oauthId = $this->oauthInfo['id'];

        $comment = CommentModel::getOne($commentId);
        if (empty($comment)) {
            return self::sendResult(118);
        }

        if ($comment['fromUid'] != $oauthId) {
            return self::sendResult(120);
        }

        CommentModel::deleteOne($commentId, $oauthId);

        return self::sendResult(self::$okCode, [], '删除成功');
    }
    
    
    /**
     * 未读消息列表
     * @return array
     * @throws \think\Exception\DbException
     */
    public function unReadMessage()
    {
        $reqData = input('');
        $model = CommentModel::unReadMessage($reqData);
        return self::sendResult(self::$okCode, $model);
    }
}